package com.lianniu.mgr.baseService.post.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.post.dao.IPostMapperDao;
import com.lianniu.mgr.baseService.post.model.Post2Menu2OpVo;
import com.lianniu.mgr.baseService.post.model.Post2MenuVo;
import com.lianniu.mgr.baseService.post.model.Post2OrgVo;
import com.lianniu.mgr.baseService.post.model.Post2SysAreaVo;
import com.lianniu.mgr.baseService.post.model.PostVo;
import com.lianniu.mgr.baseService.post.service.IPostService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PostServiceImpl implements IPostService {

  private final static Logger logger = Logger.getLogger(PostServiceImpl.class);

  @Resource
  IPostMapperDao postMapperDao;

  /**
   * @Description：查询岗位列表
   * @Param:PostVo
   * @Return:List<PostVo>

   * @Date:2016-3-24 10:31:43
   */
  @Override
  public BaseResult getPostList(Post2OrgVo post2OrgVo, PaginationInfo paginationInfo) {

    logger.info("getPostList begin");
    if (!StringUtil.isEmpty(post2OrgVo.getPostName())) {
      post2OrgVo.setPostName(post2OrgVo.getPostName().replace(" ", ""));
    }
    logger.info("post_name: " + post2OrgVo.getPostName());

    post2OrgVo.setPaginationInfo(paginationInfo);
    List<Post2OrgVo> list = postMapperDao.getPostListForPage(post2OrgVo);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);

    return BaseResult.success(retmap);
  }

  /**
   * @Description：插入岗位信息
   * @param postVo

   * @return 插入记录数
   * @Date:2016-3-24 23:20:58
   */
  @Transactional
  @Override
  public BaseResult addPostInfo(PostVo postVo) {

    logger.info("addPostInfo begin");
    logger.info("addPostInfo: input data check begin");
    logger.info("org_id: " + postVo.getOrgId());
    logger.info("post_name: " + postVo.getPostName());
    logger.info("post_desc: " + postVo.getPostDesc());
    logger.info("add_user: " + postVo.getAddUser());

    // 岗位ID的取得
    String sId = StringUtil.getUUID();
    logger.info("id: " + sId);
    postVo.setId(sId);

    BaseResult baseResult;
    if (StringUtil.isEmpty(postVo.getOrgId())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(postVo.getPostName())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if ((postVo.getPostName().length()>30)) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if ((postVo.getPostDesc().length()>200)) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("addPostInfo: input data check end");

    return BaseResult.success(postMapperDao.addPostInfo(postVo));

  }

  /**
   * @Description：根据岗位ID更新岗位信息
   * @param postVo

   * @return 更新记录数
   * @Date:2016-3-24 23:20:58
   */
  @Transactional
  @Override
  public BaseResult editPostInfo(PostVo postVo) {

    logger.info("editPostInfo begin");
    logger.info("editPostInfo: input data check begin");
    logger.info("id: " + postVo.getId());
    logger.info("org_id: " + postVo.getOrgId());
    logger.info("post_name: " + postVo.getPostName());
    logger.info("post_desc: " + postVo.getPostDesc());
    logger.info("add_user: " + postVo.getAddUser());

    BaseResult baseResult;
    if (StringUtil.isEmpty(postVo.getOrgId())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(postVo.getPostName())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if ((postVo.getPostName().length()>30)) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if ((postVo.getPostDesc().length()>200)) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("editPostInfo: input data check end");

    return BaseResult.success(postMapperDao.editPostById(postVo));
  }

  /**
   * @Description：删除新岗位信息
   * @param postVo

   * @return 更新记录数
   * @Date:2016-3-24 23:20:58
   */
  @Transactional
  @Override
  public BaseResult delPost(String modifyUser, String... id) {
    logger.info("deletePost begin");
    logger.info("deletePost: input data check begin");
    logger.info("modifyUser: " + modifyUser);
    logger.info("id: " + id.toString());
    BaseResult baseResult;
    if (id == null || id.length <= 0) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("deletePost: input data check end");
    
    postMapperDao.delPost(modifyUser, id);
    postMapperDao.delPost2MenuByPostId(id);
    postMapperDao.delPost2Menu2OpByPostId(id);
    postMapperDao.delPost2SysAreaByPostId(id);
    return BaseResult.success();
  }

  /**
   * @Description：查询岗位信息
   * @param postVo

   * @return 查询记录数
   * @Date:2016-3-24 23:20:58
   */
  @Override
  public BaseResult getPostInfoById(PostVo postVo) {

    logger.info("getPostInfoById begin");
    logger.info("id: " + postVo.getId());

    List<PostVo> list = postMapperDao.getPostInfoById(postVo);
    return BaseResult.success(list);
  }

  /**
   * @Description：根据岗位id复制岗位
   * @Param:PostVo
   * @Return:返回值

   * @Date:2016-3-31 11:02:38
   */
  @Transactional
  @Override
  public BaseResult copyPostById(PostVo postVo) {
    logger.info("copyPostById begin");
    logger.info("id: " + postVo.getId());
    logger.info("orgId: " + postVo.getOrgId());
    logger.info("postName: " + postVo.getPostName());
    logger.info("postDesc: " + postVo.getPostDesc());
    logger.info("addUser: " + postVo.getAddUser());
    BaseResult baseResult;
    logger.info("copyPostById: input data check begin");
    if (postVo == null || StringUtil.isEmpty(postVo.getId())
        || StringUtil.isEmpty(postVo.getOrgId()) || StringUtil.isEmpty(postVo.getPostName())
        || StringUtil.isEmpty(postVo.getAddUser())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("copyPostById: input data check end");
    
    // 新生成的岗位id
    String newPostId = StringUtil.getUUID();
    logger.info("newPostId=" + newPostId);
    List<Post2Menu2OpVo> post2Menu2OpList = null;
    
    // 根据被复制的岗位的岗位id查询lianniu_post2menu列表
    logger.info("getPost2MenuListByPostId begin");
    List<Post2MenuVo> post2MenuList = postMapperDao.getPost2MenuListByPostId(postVo);
    logger.info("getPost2MenuListByPostId end");
    
    // 根据被复制的岗位的岗位id查询lianniu_post2sysarea列表
    logger.info("getPost2SysAreaListByPostId begin");
    List<Post2SysAreaVo> post2SysAreaList = postMapperDao.getPost2SysAreaListByPostId(postVo);
    logger.info("getPost2SysAreaListByPostId end");
    
    // 如果获取的lianniu_post2menu列表不为空，则根据该列表遍历查询lianniu_post2menu2op列表
    logger.info("getPost2Menu2OpListByPost2MenuVo begin");
    if (!StringUtil.isEmpty(post2MenuList)) {
      for (int i = 0; i < post2MenuList.size(); i++) {
        List<Post2Menu2OpVo> post2Menu2OpTempList =
            postMapperDao.getPost2Menu2OpListByPost2MenuVo(post2MenuList.get(i));
        if(!StringUtil.isEmpty(post2Menu2OpTempList)){
          if (post2Menu2OpList==null)
            post2Menu2OpList = post2Menu2OpTempList;
          else
            post2Menu2OpList.addAll(post2Menu2OpTempList);
        }
      }
    }
    logger.info("getPost2Menu2OpListByPost2MenuVo end");
    
    // 修改要插入lianniu_post2menu列表的内容
    if (!StringUtil.isEmpty(post2MenuList)) {
      for (int i = 0; i < post2MenuList.size(); i++) {
        post2MenuList.get(i).setId(StringUtil.getUUID());
        post2MenuList.get(i).setPostId(newPostId);
      }
      // 插入lianniu_post2menu列表的内容
      logger.info("addPost2MenuBatch begin");
      postMapperDao.addPost2MenuBatch(post2MenuList);
      logger.info("addPost2MenuBatch end");
    }
    
    // 修改要插入lianniu_post2menu2op列表的内容
    if (!StringUtil.isEmpty(post2Menu2OpList)) {
      for (int i = 0; i < post2Menu2OpList.size(); i++) {
        post2Menu2OpList.get(i).setId(StringUtil.getUUID());
        post2Menu2OpList.get(i).setPostId(newPostId);
      }
      // 插入lianniu_post2menu2op列表的内容
      logger.info("addPost2Menu2OpBatch begin");
      postMapperDao.addPost2Menu2OpBatch(post2Menu2OpList);
      logger.info("addPost2Menu2OpBatch end");
    }
    
    // 修改要插入lianniu_post2sysarea列表的内容
    if (!StringUtil.isEmpty(post2SysAreaList)) {
      for (int i = 0; i < post2SysAreaList.size(); i++) {
        post2SysAreaList.get(i).setId(StringUtil.getUUID());
        post2SysAreaList.get(i).setPostId(newPostId);
      }
      // 插入lianniu_post2sysarea列表的内容
      logger.info("addPost2SysAreaBatch begin");
      postMapperDao.addPost2SysAreaBatch(post2SysAreaList);
      logger.info("addPost2SysAreaBatch end");
    }
    
    postVo.setId(newPostId);
    // 插入岗位表
    logger.info("addPostInfo begin");
    postMapperDao.addPostInfo(postVo);
    logger.info("addPostInfo end");
    logger.info("copyPostById end");
    return BaseResult.success();
  }
  
  /**
   * @Description：根据岗位id查询岗位组织信息
   * @Param:Post2OrgVo
   * @Return:返回值

   * @Date:2016-4-1 11:02:38
   */
  @Override
  public BaseResult getPost2OrgById(Post2OrgVo post2OrgVo) {
    logger.info("getPost2OrgById begin");
    logger.info("id: " + post2OrgVo.getId());
    BaseResult baseResult;
    if (StringUtil.isEmpty(post2OrgVo.getId())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("getPost2OrgById begin");
    List<Post2OrgVo> list = postMapperDao.getPost2OrgById(post2OrgVo);
    logger.info("getPost2OrgById end");
    return BaseResult.success(list);
  }
  
  /**
   * @Description：根据菜单id列表，返回已经分配岗位的菜单id列表
   * @Param:List<String> 菜单id列表
   * @Return:List<String> 已经分配岗位的菜单id列表

   * @Date:2016-4-5 13:57:43
   */
  @Override
  public BaseResult isAllocatedPosts(List<String> menuIdList) {
    logger.info("jusifyIsAllovatedPosts begin");
    logger.info("id: " + menuIdList.toString());
    BaseResult baseResult;
    if (StringUtil.isEmpty(menuIdList)) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("jusifyIsAllovatedPosts begin");
    List<String> list = postMapperDao.isAllocatedPosts(menuIdList);
    logger.info("jusifyIsAllovatedPosts end");
    return BaseResult.success(list);
  }
}
